﻿using FastCloud.Base;
using FastCloud.Base.Config;
using FastCloud.Base.Desgin;
using FastCloud.ORG.DB;
using FastCloud.ORG.Entity;
using FastIOC.Attribute;
using FastORM.Entity;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.ORG
{
    [Route("frame/org/ou")]
    public class OUController : BaseController
    {
        [AutoWired]
        private DB_OU DB_OU;
        [AutoWired]
        private DB_ModuleRight DB_ModuleRight;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/ORG/OU/OU_List.cshtml");
        }

        [Route("add")]
        public IActionResult Add()
        {
            return View("~/Views/ORG/OU/OU_Add.cshtml");
        }

        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/ORG/OU/OU_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string OUName = ValueConvert.ConvertToString(GetFormParam("OUName"));
            string ParentOUGuid = ValueConvert.ConvertToString(GetFormParam("ParentOUGuid"));
            bool SearchInAll = ValueConvert.ConvertToBoolean(GetFormParam("SearchInAll"));
            string sql = "1=1";
            List<ParamEntity> ParamList = new List<ParamEntity>();
            if (!string.IsNullOrEmpty(OUName))
            {
                sql += " and OUName like @OUName";
                ParamList.Add(new ParamEntity() { ParamName = "OUName", ParamValue = "%" + OUName + "%" });
            }
            if (!SearchInAll)
            {
                if (!string.IsNullOrEmpty(ParentOUGuid))
                {
                    sql += " and ParentOUGuid=@ParentOUGuid";
                    ParamList.Add(new ParamEntity() { ParamName = "ParentOUGuid", ParamValue = ParentOUGuid });
                }
            }
            return Result.OK(DB_OU.SelectPage("*", sql, "SortNum desc",ParamList));
        }

        [Route("opinsert"), HttpPost]
        [Transitional(TransitonLevel = IsolationLevel.ReadUncommitted)]
        public virtual IActionResult OpInsert(Frame_OU Entity)
        {
            DB_OU.Insert(Entity);
            return Result.OK();
        }

        [Route("opupdate"), HttpPost]
        [Transitional(TransitonLevel = IsolationLevel.ReadUncommitted)]
        public virtual IActionResult OpUpdate(Frame_OU Entity)
        {
            DB_OU.UpdateByID(Entity);
            return Result.OK();
        }

        [Route("opdetail"), HttpPost]
        public IActionResult OpDetail()
        {
            string OUGuid = ValueConvert.ConvertToString(GetFormParam("OUGuid"));
            return Result.OK(DB_OU.SelectOne(OUGuid));
        }

        [Route("opbatchdelete"), HttpPost]
        [Transitional(TransitonLevel = IsolationLevel.ReadUncommitted)]
        public virtual IActionResult OpBatchDelete()
        {
            string KeyID = ValueConvert.ConvertToString(GetFormParam("KeyID"));
            List<string> KeyIDList = KeyID.TrimEnd(';').Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            foreach (var item in KeyIDList)
            {
                Frame_OU ou = DB_OU.SelectOne(item);
                if (ou.SubOUNum > 0)
                {
                    return Result.Error(string.Format("部门{0}包含子部门，无法删除", ou.OUName));
                }
                if (ou.SubUserNum > 0)
                {
                    return Result.Error(string.Format("部门{0}包含用户，无法删除", ou.OUName));
                }
            }
            //删除模块权限
            foreach (string RoleGuid in KeyIDList)
            {
                DB_ModuleRight.DeleteByFiled("RightGuid", RoleGuid);
            }
            DB_OU.DeleteBatch(KeyIDList);
            return Result.OK();
        }

        [Route("tree"), HttpPost]
        public IActionResult Tree()
        {
            List<Frame_OU> GroupList = DB_OU.SelectAllList();
            GroupList.Sort((x, y) => -x.SortNum.CompareTo(y.SortNum));
            List<TreeNode> NodeList = new List<TreeNode>();
            Frame_OU Root = new Frame_OU();
            Root.OUGuid = "Top";
            Root.OUName = "全部部门";
            Root.ParentOUGuid = "";
            GroupList.Add(Root);
            foreach (Frame_OU item in GroupList)
            {
                TreeNode node = new TreeNode();
                node.ID = item.OUGuid;
                node.Name = item.OUName;
                node.PID = item.ParentOUGuid;
                if (item.OUGuid == Root.OUGuid)
                {
                    node.IsTop = true;
                    node.Open = true;
                }
                NodeList.Add(node);
            }
            return Result.OK(CloudUtil.BuildTree(NodeList));
        }
    }
}
